#include <QCheckBox>
#include <QPushButton>
#include <QHBoxLayout>
#include <QKeyEvent>

#include "taskmonitorwidget.h"

#include "treeview.h"
#include "propselector.h"

static const int DEFAULT_WIDTH = 800; // 任务监视器的默认宽度
static const int DEFAULT_HEIGHT = 600; // 任务监视器的默认高度

TaskMonitorWidget::TaskMonitorWidget(QWidget * parent, Qt::WindowFlags f)
    : QWidget(parent, f)
{
    m_tasktree.setTreeMode(true);
    m_tasktree.flush();

    m_treemodel = new TreeModel(&m_tasktree, this);
    m_treemodel->setAutoFlush(true);

    TreeView *tree_view = new TreeView(m_treemodel, this);

    QCheckBox *cb_tree_mode = new QCheckBox;
    cb_tree_mode->setText(tr("Use Tree Mode"));
    cb_tree_mode->setChecked(m_tasktree.isTreeMode());
    QObject::connect(cb_tree_mode, SIGNAL(clicked(bool)), m_treemodel, SLOT(setTreeMode(bool)));

    QCheckBox *cb_auto_flush = new QCheckBox;
    cb_auto_flush->setText(tr("Auto Flush"));
    cb_auto_flush->setChecked(m_treemodel->isAutoFlush());
    QObject::connect(cb_auto_flush, SIGNAL(clicked(bool)), m_treemodel, SLOT(setAutoFlush(bool)));

    QPushButton *btn_select_columns = new QPushButton;
    btn_select_columns->setText(tr("Select Colums"));
    PropSelector *prop_selector = new PropSelector(tree_view);
    btn_select_columns->setMenu(prop_selector);

    QPushButton *btn_flush = new QPushButton;
    btn_flush->setText(tr("Flush"));
    QObject::connect(btn_flush, SIGNAL(clicked(bool)), m_treemodel, SLOT(flush()));

    QHBoxLayout *control = new QHBoxLayout;
    control->addWidget(cb_tree_mode);
    control->addWidget(cb_auto_flush);
    control->addStretch();
    control->addWidget(btn_flush);
    control->addWidget(btn_select_columns);

    TaskLocator *locator = new TaskLocator(m_treemodel, tree_view);

    QHBoxLayout *foot_control = new QHBoxLayout;
    foot_control->addWidget(locator);
    foot_control->addStretch(2);

    QVBoxLayout *main_layout = new QVBoxLayout;
    main_layout->addLayout(control);
    main_layout->addWidget(tree_view);
    main_layout->addLayout(foot_control);

    setLayout(main_layout);

    resize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
}

TaskLocator::TaskLocator(TreeModel *model, TreeView *view, QWidget *parent)
    : QLineEdit(parent),
      m_model(model),
      m_view(view)
{
    setPlaceholderText(tr("Locate Task ."));
    setToolTip(tr("Find And Show A Task ."));
}

void TaskLocator::keyPressEvent(QKeyEvent *e)
{
    QLineEdit::keyPressEvent(e);
}

void TaskLocator::keyReleaseEvent(QKeyEvent *e)
{
    const int key = e->key();

    if (key == Qt::Key_Return || key == Qt::Key_Enter) { // Qt::Key_Return/Qt::Key_Enter 为键盘上不同位置的回车键

        if (e->isAutoRepeat()) { // 是否是按住不放导致的事件，如果是则忽略，只有是最终真正松开按键才处理
            return;
        }

        QModelIndex index = m_model->getIndexByPid(text().toInt());
        if (index.isValid()) {
            m_view->setCurrentIndex(index);
            m_view->scrollTo(index, QTreeView::PositionAtTop);
            selectAll();
        }
    }

    QLineEdit::keyReleaseEvent(e);
}
